package my

import java.net.URLEncoder

import akka.actor.{Actor, ActorLogging, ActorRef}
import my.MyMaster.StartJob
import my.MyReceptionist.{Job, JobRequest}

/**
  * Created by shichen on 2017/8/3.
  */
object MyReceptionist {
  case class JobRequest(name: String,text: List[String])

  case class Job(name: String, text: List[String],respondTo: ActorRef, jobMaster: ActorRef)
}

trait CreateMaster { this: Actor =>
  def createMaster(name: String) = context.actorOf(MyMaster.props(),name)
}

class MyReceptionist extends Actor with ActorLogging
  with CreateMaster {

  var jobs = Set[Job]()

  override def receive: Receive = {
    case JobRequest(name,text) =>
      log.info(s"recv job $name")
      val masterName = "master-" + URLEncoder.encode(name,"UTF8")
      val master = createMaster(masterName)

      val job = Job(name,text,sender(),master)
      //这里封装成Job，难道就只是给jobs填充一个元素吗？
      //不用发出去？
      jobs = jobs + job
      master ! StartJob(name,text)
  }
}